<template>
  <div>
    <el-dialog
      :title="form.id ? '编辑部门' : '新增部门'"
      width="50%"
      :visible="showadddept"
      @close="closeDia"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
        :inline="false"
        size="normal"
      >
        <el-form-item label="部门名称" prop="name">
          <el-input
            v-model="form.name"
            style="width: 80%"
            placeholder="1-50个字符"
          />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input
            v-model="form.code"
            style="width: 80%"
            placeholder="1-50个字符"
          />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select
            v-model="form.manager"
            placeholder="请选择"
            style="width: 80%"
          >
            <el-option
              v-for="manager in menagerList"
              :key="manager.id"
              :label="manager.username"
              :value="manager.username"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input
            v-model="form.introduce"
            type="textarea"
            style="width: 80%"
            placeholder="1-300个字符"
          />
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button @click="$emit('close-dia')">取消</el-button>
        <el-button type="primary" @click="addDeptSubmit">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
export default {
  props: {
    showadddept: {
      type: Boolean,
      default: false
    },
    depslist: {
      type: Array,
      default: () => []
    },
    currentdep: {
      type: Object,
      default: () => ({})
    }

  },
  data () {
    // 排除重复的code
    const validateCode = (rules, value, callback) => {
      let flag
      if (this.form.id) {
        flag = this.depslist.some(item => item.code === value && value !== this.currentdep.code)
        console.log('编辑', flag)
        console.log(this.currentdep)
      } else {
        flag = this.depslist.some(item => item.code === value)
        console.log('添加', flag)
      }

      if (flag) {
        callback(new Error('存在重复的code'))
      } else {
        callback()
      }
    }
    return {
      menagerList: [],
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: validateCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created () {
    this.getMenegersList()
  },
  methods: {
    // 关闭弹层
    closeDia () {
      this.$emit('close-dia')
      this.$refs.form.resetFields()
      this.form = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
    },
    // 获取manager
    async getMenegersList () {
      const res = await getEmployeeSimple()
      this.menagerList = res
    },
    async addDeptSubmit () {
      await this.$refs.form.validate()
      try {
        if (this.form.id) {
          await updateDepartments(this.form)
          this.$message.success('修改部门成功')
        } else {
          // 复用添加api
          await addDepartments({ ...this.form, pid: this.currentdep.id || '' })
          this.$message.success('添加部门成功')
        }
        this.closeDia()
      } catch (error) {
        console.log(error)
      }
    },
    async editDepart (id) {
      const res = await getDepartDetail(id)
      this.form = res
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-form-item__error {
  color: red;
}
</style>
